Librerías Cargamos las librerías que vamos a utilizar.

library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(dplyr)
library(showtext)
## Warning: package 'showtext' was built under R version 4.3.3
## Loading required package: sysfonts
## Warning: package 'sysfonts' was built under R version 4.3.3
## Loading required package: showtextdb

Importación de datos Cargamos en una variable los datos de la web.

#Nos aseguramos de que el enviroment esté limpio
rm(list = ls())
data <- read.csv("https://covid.ourworldindata.org/data/owid-covid-data.csv")

Modificación de variables Cambiamos el formato de las variables para poder manejarlas de mejor manera.

#Ponemos el formato de fecha adecuado
data$date <- as.Date(data$date, format = "%Y-%m-%d")

#Creamos un vector con los paises que queremos representar
paises <- c("Spain","Italy", "Germany","Canada","United States",
            "Japan","United Kingdom", "France")

#Creamos un nuevo df con las variables que necesitamos para el gráfico
paises <- data %>% 
  filter(location %in% paises) %>%
  group_by(location, date) %>% 
  summarise(muertes = new_deaths_smoothed_per_million) 
## `summarise()` has grouped output by 'location'. You can override using the
## `.groups` argument.
#Una df distinta para después agregar el grosor de linea
Spain <- data %>% 
  filter(location == "Spain") %>%
  group_by(location, date) %>% 
  summarise(muertes = new_deaths_smoothed_per_million) 
## `summarise()` has grouped output by 'location'. You can override using the
## `.groups` argument.

Gráfico Creamos el grafico con la librería ‘ggplot2’

#Primero creamos un vector con los colores que vamos a utilizar
colores <- c("Spain"="red2","Italy" = "cyan4",
              "Germany" = "palegreen4",
             "Canada" = "orangered4", "Japan" = "skyblue4", 
             "United Kingdom" = "maroon", "France" = "sienna",
             "United States" = "darkorchid4")

#Ponemos las fuentes de texto que vamos a utilizar
## Loading Google fonts (https://fonts.google.com/)
font_add_google("Roboto", "Roboto")
font_add_google("EB Garamond", "gara")

showtext_auto()


#Creamos el gráfico
graf <- ggplot(paises, aes(x = date,y = muertes, color = location)) +
  geom_line() + geom_line(mapping = aes(x = date, y = muertes),
                          colour = "red2", data = Spain, linewidth = 1 ) +
  scale_colour_manual(values = colores) + 
  labs(title = "Grupo T\nDaily new confirmed COVID-19 deaths per million people", 
subtitle = "7-day rolling average. For some countries the number of confirmed deaths is much lower than the true number of deaths. This is because of limited\ntesting and challenges in the attribution of the couse of death.",
x = NULL, y = NULL, 
caption = "Source:Johns Hopkins University CSSE COVID-19 Data") + 
  scale_y_continuous(breaks = seq(0,20,2)) +
  theme(plot.title = element_text(hjust = 0,
                                  size = 15, 
                                  colour = "#222222",
                                  family = "gara"),
        plot.subtitle = element_text(hjust = 0,
                                     size = 6.5,
                                     colour = "#757575",
                                     family = "Roboto"),
        plot.caption.position = "plot",
        plot.caption = element_text(hjust = -0.01,
                                    colour = "#757575",
                                    family = "gara",
                                    size = 6),
        panel.grid.major.y = element_line(colour = "grey", linetype = 2),
        panel.background = element_blank())
graf
## Warning: Removed 759 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 229 rows containing missing values or values outside the scale range
## (`geom_line()`).

ggsave("Grafico.pdf", plot = graf)
## Saving 7 x 5 in image
## Warning: Removed 759 rows containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 229 rows containing missing values or values outside the scale range
## (`geom_line()`).

Gráfico interactivo

grafite <- ggplotly(graf)
grafite